package pl.edu.agh.uddiProxy.client.login;

import java.util.Arrays;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.gwtent.acegi.client.UserLoginRpc;

public class Login implements EntryPoint {

	final Button loginButton = new Button("Login");
	//final Button logoutButton = new Button("Logout");
	final TextBox userNameText = new TextBox();
	final PasswordTextBox passwordText = new PasswordTextBox();
	final CheckBox cbRememberMe = new CheckBox("Remember Me");

	public Login() {
		getUser();
	}
	
	public void onModuleLoad() {
		KeyboardListenerAdapter keyboardListenerAdapter = new KeyboardListenerAdapter() {
			@Override
			public void onKeyDown(Widget sender, char keyCode, int modifiers) {
				if (keyCode == KeyboardListener.KEY_ENTER) {
					loginButton.click();
				}
				super.onKeyDown(sender, keyCode, modifiers);
			}
		};
		passwordText.addKeyboardListener(keyboardListenerAdapter);
		userNameText.addKeyboardListener(keyboardListenerAdapter);
		
		loginButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				UserLoginRpc.Util.getInstance().login(userNameText.getText(),
						passwordText.getText(), cbRememberMe.isChecked(),
						new AsyncCallback<Boolean>() {

							public void onFailure(Throwable caught) {
								caught.printStackTrace();
								Window.alert(caught.toString());
							}

							public void onSuccess(Boolean result) {
								if (result) {
									login();
								} else {
									Window.alert("Login Failed");
								}
							}

						});
			}

		});

		/*logoutButton.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				UserLoginRpc.Util.getInstance().logout(userNameText.getText(), new AsyncCallback() {
					public void onFailure(Throwable caught) {
						caught.printStackTrace();
						Window.alert(caught.toString());
						getUser();
					}

					public void onSuccess(Object result) {
						Window.alert("Logout successfully");
						getUser();
					}

				});
			}

		});*/
		
		Grid grid = new Grid(2, 2);
		int i = 0;
		
		grid.setText(i, 0, "Username");
		grid.setWidget(i++, 1, userNameText);
		grid.setText(i, 0, "Password");
		grid.setWidget(i++, 1, passwordText);
		
		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.add(grid);
		verticalPanel.add(cbRememberMe);

		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.add(loginButton);
		//horizontalPanel.add(logoutButton);
		horizontalPanel.setSpacing(5);
		
		verticalPanel.add(horizontalPanel);
		
		RootPanel.get("panel").add(verticalPanel);
	}

	private void getUser() {
		UserLoginRpc.Util.getInstance().getCurrentUserName(new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}
	
			public void onSuccess(String username) {
				if (username != null && !username.equals("ANONYMOUSUSER")) {
					login();
				}
			}
		});
	}

	private void login() {
		Window.open("../Application/Application.html", "_self", null);
	}
}
